package com.hangzhou.xyj.ui.widget.views;

import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.ViewConfiguration;

/**
 * Created by yaohuix on 2016/5/18  16:57
 * <p/>
 * 说明：自定义 ViewPager
 */
public class MyViewPager extends ViewPager {
    private boolean mCanLeft = true;//是否可以左滑
    private Context mContext;

    private float startX = 0;

    public MyViewPager(Context context) {
        super(context);
        this.mContext = context;
    }

    public MyViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;
    }

    public void setCanLeft(boolean isCanLeft) {
        this.mCanLeft = isCanLeft;
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        return super.dispatchTouchEvent(ev);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        long touchSlop = ViewConfiguration.get(mContext).getScaledTouchSlop();//最小的滑动距离
        if (!mCanLeft) {//拦截左滑
            switch (ev.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    startX = ev.getRawX();
                    break;
                case MotionEvent.ACTION_MOVE:
                    if (startX - ev.getRawX() > touchSlop) {//左滑
                        return false;//不能左滑了
                    }
                    break;
                case MotionEvent.ACTION_UP:
                    break;
            }
        }
        return super.onInterceptTouchEvent(ev);
    }
}
